package org.jobtask.schedule;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultStyledDocument;

import org.jobtask.config.JobTaskConfig;
import org.jobtask.config.SystemIdentification;

public class JobTask  implements Serializable{
	

	/**
	 * 序列化版本号
	 */
	private static final long serialVersionUID = -5650577514571478078L;
	
	
	private String taskName;
	private String taskFile;
	private String taskType;
	private JobState state;
	private Integer count=null;
	private Integer interval=null;
	private HashMap<Date,String> sartTimes;
	private DefaultStyledDocument buffer;
	private long sequece;
	private int exitState=0;//0 为正常退出;1为非正常退出
	
	/**
	 * 初始化时间列表,初始化状态,初始化文档模型
	 */
	{
		sartTimes=new HashMap<Date, String>();
		state=JobState.STOP;
		buffer=new DefaultStyledDocument();
	}
	
	/**
	 * 使用默认的构造时,将创建新的任务ID
	 */
	public JobTask()
	{
		sequece=System.currentTimeMillis();
	}
	
	/**
	 * 使用传入的ID构建任务
	 */
	public JobTask(long seq)
	{
		sequece=seq;
	}
	
	
	public String getSequece() {
		return sequece+"";
	}
	
	public void addLog(String line) throws BadLocationException {
		buffer.insertString(buffer.getLength(), line+"\n",null);
	}
	
	public void clearLog(boolean rm) throws BadLocationException{
		if(rm)
		    this.buffer.remove(0,buffer.getLength());
		else
			this.buffer.remove(0,1);
	}
	public DefaultStyledDocument getLogDocument(){
		return this.buffer;
	}

	
	public JobState getState() {
		return state;
	}
	
	public int getRunStatus(){
		return this.state.equals(JobState.RUNING)||this.state.equals(JobState.PAUSE)?1:0;
	}

	public void setState(JobState state) {
		this.state = state;
	}
	public String getTaskName() {
		return taskName;
	}
	public void setTaskName(String taskName) {
		this.taskName = taskName;
	}
	public String getTaskFile() {
		return taskFile;
	}
	public void setTaskFile(String taskFile) {
		this.taskFile = taskFile;
	}
	public Integer getCount() {
		return count;
	}
	public void setCount(Integer count) {
		this.count = count;
	}
	public Integer getInterval() {
		return interval;
	}
	public void setInterval(Integer interval) {
		this.interval = interval;
	}
	
	public HashMap<Date,String> getSartTimes() {
		HashMap<Date,String> temp=new HashMap<Date, String>();
		String currentDate=SystemIdentification.parseDateTimeToString(new Date(),false);
		for(Iterator<Date> iter=this.sartTimes.keySet().iterator();iter.hasNext();){
			Date dateKey=iter.next();
			temp.put(SystemIdentification.parseStringToDateTime(currentDate+" "+SystemIdentification.parseTimeToString(dateKey,true)),this.sartTimes.get(dateKey));
		}
		this.sartTimes=temp;
		return this.sartTimes;
	}
	
	public void addStartTime(Date time,String method){
		this.sartTimes.put(time,method);
	}
	
	public  String[] getCommand(){
		String cmd=JobTaskConfig.getString(this.taskFile.substring(this.taskFile.lastIndexOf(".")+1))+","+this.taskFile;
		return cmd.split(",");
	}
	
	public String getTaskType() {
		return taskType;
	}
	public void setTaskType(String taskType) {
		this.taskType = taskType;
	}
	@Override
	public String toString() {
		return this.taskName;
	}
	
	public void reset(){
		try {
			this.clearLog(true);
		} catch (BadLocationException e) {
			e.printStackTrace();
		}
		this.state=JobState.STOP;
	}
	
	public Date[] getSortKeys(){
		Object[] keySet=this.getSartTimes().keySet().toArray();
		Arrays.sort(keySet);
		Date[] d=new Date[keySet.length];
		for(int index=0;index<keySet.length;index++)
			  d[index]=(Date)keySet[index];
		return d;
	}
	
	public ArrayList<String[]> taskInfo(){
		ArrayList<String[]> rows=new ArrayList<String[]>();
		rows.add(new String[]{"任务编码",this.sequece+""});
		rows.add(new String[]{"任务名",this.taskName});
		rows.add(new String[]{"文件路径",this.taskFile});
		rows.add(new String[]{"当前状态", this.state.equals(JobState.RUNING)?"运行":"停止"});
		if(this.taskType.equals("SCHEDULING.REPEAT")){
			rows.add(new String[]{"运行方式", "重复任务"});
			rows.add(new String[]{"运行次数",this.count==0?"始终运行":this.count+"次"});
			rows.add(new String[]{"间隔时间",this.interval+"秒"});
		}else{
			rows.add(new String[]{"运行方式", "定时任务"});
			Date[] dateSet=this.getSortKeys();
			for(int index=0;index<dateSet.length;index++){
			     rows.add(new String[]{SystemIdentification.parseDateTimeToString(dateSet[index],true),this.sartTimes.get(dateSet[index]).equals("one")?"一次":"每天"});
			}
		}
		return rows;
	}
	
//	public HashMap<Date,String> afterTime(){
//		HashMap<Date, String> map=new HashMap<Date, String>();
//		Date currentTime=new Date();
//		for(Iterator<Date> iter=this.sartTimes.keySet().iterator();iter.hasNext();){
//			Date dt=iter.next();
//
//		}
//	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (int) (sequece ^ (sequece >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		JobTask other = (JobTask) obj;
		if (sequece != other.sequece)
			return false;
		return true;
	}
}
